<template>
  <div>

    <el-form :inline="true" :model="search" class="user-search">
      <el-form-item label="搜索">
        <el-input size="small" v-model="search.carId" placeholder="车牌号"></el-input>
      </el-form-item>
      <el-form-item>
        <el-input size="small" v-model="search.location" placeholder="车位编号"></el-input>
      </el-form-item>
      <el-form-item>
        <el-input size="small" v-model="search.price" placeholder="付款价格"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button size="small" type="primary" icon="el-icon-search" @click="sel()">搜索</el-button>
      </el-form-item>
    </el-form>

    <el-table
        :data="tableData"
        style="width: 100%"
        :default-sort="{prop: 'date', order: 'descending'}"
    >
      <el-table-column prop="id" label="编号" header-align="center" align="center"></el-table-column>
      <el-table-column prop="location" label="车位编号" header-align="center" align="center"></el-table-column>
      <el-table-column prop="carId" label="车牌号" header-align="center" align="center"></el-table-column>
      <el-table-column prop="inTime" sortable label="入场时间" header-align="center" align="center"></el-table-column>
      <el-table-column prop="outTime" sortable label="出场时间" header-align="center" align="center"></el-table-column>
      <el-table-column prop="stopTime" sortable label="停车时间（小时）" header-align="center" align="center"></el-table-column>
      <el-table-column prop="price" sortable label="停车费用" header-align="center" align="center"></el-table-column>
    </el-table>

    <el-pagination @size-change="sizeChange" @current-change="currentChange"
                   :current-page="page" :page-size="size" :page-sizes="pageSizes"
                   layout="total, sizes, prev, pager, next, jumper" :total="total">
    </el-pagination>
  </div>
</template>

<script>
export default {
  data() {
    return {
      tableData: [],
      search: {},
      page: 1, //第几页
      size: 10, //一页多少条
      total: 0, //总条目数
      allData:[],
      pageSizes: [10, 20, 30], //可选择的一页多少条
    };
  },
  methods: {


    //获取表格数据，自行分页（splice）
    getTabelData2() {
      let data = JSON.parse(JSON.stringify(this.allData))
      this.tableData = data.splice(
          (this.page - 1) * this.size,
          this.size
      );
      this.total = this.allData.length
    },
    //page改变时的回调函数，参数为当前页码
    currentChange(val) {
      console.log("翻页，当前为第几页", val);
      this.page = val;
      this.getTabelData2();
    },
    //size改变时回调的函数，参数为当前的size
    sizeChange(val) {
      console.log("改变每页多少条，当前一页多少条数据", val);
      this.size = val;
      this.page = 1;
      this.getTabelData2();
    },
    sel() {
      let url = 'http://localhost:8080/car/search'
      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .post(url, this.search).then((response) => {
        console.log(this.search)
        let responseBody = response.data;
        if (responseBody.code == 200) {
          this.tableData = responseBody.data;
        } else {
          this.$message({
            showClose: true,
            message: responseBody.message,
            type: 'error'
          });
        }
      });
    },

    //请求车位列表
    loadParkingSpaceList() {
      let url = 'http://localhost:8080/car/getHistory'
      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .get(url).then((response) => {
        let responseBody = response.data;
        console.log(this.editForm)
        if (responseBody.code == 200) {
          this.allData = responseBody.data;
          this.tableData = this.allData.slice(
              (this.page - 1) * this.size,
              this.page * this.size
          );
          this.total=this.allData.length
          console.log(this.tableData)
        } else {
          this.$message({
            showClose: true,
            message: responseBody.message,
            type: 'error'
          });
        }
      });

    }
  }
  ,
  created() {
    this.loadParkingSpaceList()
  }
}
</script>
